#include <iostream>
#include <fstream>
#include <sstream>
#include "teebuf.h"

using namespace std;

int main(int argc,char *argv)
{
    char *log_fn = "test_teebuf.log";
    cerr << "0 hello, this is test_teebuf\nneverything normal so far\n\n";

    ofstream logs(log_fn);
    logs << "1 this was written before any redirection took place\n"
	 << "  (and should only appear in the log file)\n";
    logs.flush();

    stringstream ss;

    ostream log( new teebuf(cerr.rdbuf(),ss.rdbuf()) );
    log << "2 this was written to log, which should be redirected to cerr\n";

    logs << ss.str();

    log.rdbuf( new teebuf(cerr.rdbuf(),logs.rdbuf()) );
    log << "3 this is written to log and should be printed on screen"
	<< " as well a written to the logfile\n";

    logs.close();
}
